var sleep = function(time) {
    return new Promise((resolve, reject)=> {
        setTimeout(()=> {
            if(time >= 1000){
                resolve("function sleep is invoked after " + time + " ms");
            }else{
                reject("function sleep is invoked after " + time + "ms, sleepTime should be longer");
            }
        }, time);
    });
};

//var commonInvoke = ()=> {
//    sleep(1200).then((info)=>{
//        console.log('commonInvoke');
//        console.log(info);
//        console.log('-------------------------------------------------');
//    });
//};
//commonInvoke();

var asyncInvoke = async ()=> {
   console.log('async function begin');
   var result = await sleep(1100);
   console.log(result);
   console.log('after await => async end with success');
   console.log('-------------------------------------------------');
};
asyncInvoke();

//var asyncInvokeRejected = async ()=> {
//    try {
//        console.log('async function begin');
//        var result = await sleep(500);
//        console.log(result);
//        console.log('after await => async end with success');
//    } catch(err) {
//        console.log(err);
//        console.log('after await => async end with error');
//    }
//    console.log('-------------------------------------------------');
//};
//asyncInvokeRejected();

function getTime() {
    return new Date().getTime();
}

var asyncInvokeLoop = async ()=> {
    for(var i = 0; i< 4; i++) {
        console.log('invoked ' + i + " times");
        await sleep(1000);
        console.log(getTime());
    }
};

asyncInvokeLoop();

// http://cnodejs.org/topic/5640b80d3a6aa72c5e0030b6
